package ln.gather.jsr.rs.rest;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

/**
 * <pre>
 *  RestClientDemo
 * </pre>
 *
 * @Author zhangj
 * @Date 2022/11/6 15:57
 */
public class RestClientDemo {

    public static void main(String[] args) {
        // 创建客户端实例
        // newBuilder().build()
        // SPI 找到 ClientBuilder 的实现来创建 Client
        // Client 需要实现，实现类为 DefaultClient
        Client client = ClientBuilder.newClient();
        // 客户都啊实例请求地址获取网络目标
        // 通过 URI 获取 WebTarget 实例
        // URI.create(uri) ==> URI
        // UriBuilder.fromUri(uri) ==> UriBuilder
        // - UriBuilder 是抽象类，需要子类 DefaultUriBuilder 来实现，DefaultUriBuilder维护了URI中属性信息
        // new ImmutableWebTarget(uriBuilder) ==> ImmutableWebTarget, 为 WebTarget 的实现类
        WebTarget webTarget = client.target("http://www.baidu.com");
        // new DefaultInvocationBuilder(uriBuilder) ==> Invocation.Builder 实现类为 DefaultInvocationBuilder
        Invocation.Builder request = webTarget.request();
        // buildGet() ==> Invocation
        // - buildGet() = new HttpGetInvocation(uriBuilder.build(), headers)
        // Invocation##invoke() = HttpGetInvocation##invoke()
        // HttpURLConnection 获取连接，设置相关参数，并返回结果 Response，实现类为 DefaultResponse
        Response response = request.get();

        System.out.println("response: " + response);

        String content = response.readEntity(String.class);

        System.out.println(content);

    }

}
